//
//  ByShopCarVC.m
//  hutouben_ios
//
//  Created by pro on 16/12/1.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopCarVC.h"

// cell
#import "ByShopCarCell.h"

@interface ByShopCarVC () <UITableViewDelegate, UITableViewDataSource>

// view
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIView *toolBar;          // 工具栏


// data
@property (nonatomic, strong) NSMutableArray *selectArray;
@property (nonatomic, strong) NSMutableArray *countArray;

@end

@implementation ByShopCarVC

#pragma mark - 懒加载
- (NSMutableArray *)selectArray {
    
    if (!_selectArray) {
        _selectArray = [[NSMutableArray alloc] init];
    }
    return _selectArray;
}
- (NSMutableArray *)countArray {
    
    if (!_countArray) {
        _countArray = [[NSMutableArray alloc] init];
    }
    return _countArray;
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"购物车";
    
    [self addLeftBackBarButtonItem];
    
    [self createToolBar];
    [self createTableView];
    
    // TODO:实际要根据商品数目来
    [self.selectArray addObject:@NO];
    [self.selectArray addObject:@NO];
    [self.selectArray addObject:@NO];
    [self.countArray addObject:@1];
    [self.countArray addObject:@1];
    [self.countArray addObject:@1];
}

#pragma mark - create view
// 创建工具栏
- (void)createToolBar {
    
    self.toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    self.toolBar.bottom = SCREEN_HEIGHT - 64;
    self.toolBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.toolBar];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, self.toolBar.height)];
    label.adjustsFontSizeToFitWidth = YES;
    label.font = FONT(14);
    label.text = @"总计：￥1000";
    label.textColor = HTB_THEME_COLOR;
    label.textAlignment = NSTextAlignmentLeft;
    [self.toolBar addSubview:label];
    
    // 加入购物车按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(SCREEN_WIDTH - 80, 0, 80, self.toolBar.height);
    addBtn.backgroundColor = HTB_THEME_COLOR;
    addBtn.titleLabel.font = FONT(15);
    [addBtn setTitle:@"去结算" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(gotoPay) forControlEvents:UIControlEventTouchUpInside];
    [self.toolBar addSubview:addBtn];
    
    
}

// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.toolBar.height) style:UITableViewStylePlain];
    self.table.backgroundColor = TABLE_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableFooterView = [UIView new];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByShopCarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carCell"];
    if (cell == nil) {
        cell = [[ByShopCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"carCell"];
    }
    
    cell.select = [self.selectArray[indexPath.section] boolValue];
    cell.count = self.countArray[indexPath.section];
    
    __weak typeof(cell) weakCell = cell;
    cell.modifyCountAction = ^ (NSInteger count) {
        [self.countArray replaceObjectAtIndex:indexPath.section withObject:@(count)];
        [self.table reloadData];
    };
    cell.selectAction = ^ {
        [self.selectArray replaceObjectAtIndex:indexPath.section withObject:@(!weakCell.select)];
        [self.table reloadData];
    };
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}


// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - 点击事件
- (void)gotoPay {
    
}


@end
